app.controller('five', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //详情页
    $scope.one = function (caseId) {
        // $state.go(one.stateName);
        sixteen.params.caseId = $scope.caseId;
        sixteen.params.jumpFlag = "1";   //判断是从附件跳转后进行会签
        $state.go(sixteen.stateName);
    }
    //提交数据
    $scope.submit = function () {
        if(!$scope.caseId){
            return;
        }
        var remark = $(".remark").val();
        if(remark.length > 200){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注字数不能超过200个字"));
            return;
        }
        if(remark.length == 0 && $scope.imageUrls.length == 0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注或拍照不能为空"));
            return;
        }
        if($scope.imageUrls.length!=0 &&$scope.imageUrls.length < 3 || $scope.imageUrls.length > 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "拍照最少3张，最多9张"));
            return;
        }
        $scope.delayReason = $(".reason").val();

        // $scope.delayOptionItem = $(".reason").val();

        var postData = {
            "caseId": $scope.caseId,
            "ocrConfigPage": $scope.ocrConfigPage,
            "ocrStatusReportPage": $scope.ocrStatusReportPage,
            "ocrSupplyStatusPage": $scope.ocrSupplyStatusPage,
            "ocrUsageInfoPage": $scope.ocrUsageInfoPage,
            "imageUrls": $scope.imageUrls,
            "remark":remark,
            "delayReason":$scope.delayReason,//案例提交延迟原因
            "delayOptionItem":$scope.delayOptionItem,//案例提交延迟选项
            "feSignUrl": "",
            "customerSignUrl": "",
            "customerLongitude":$scope.blhLong,
            "customerLatitude":$scope.blhLat
        };

        // alert(JSON.stringify(postData));
        // $scope.postData = JSON.stringify(postData);

        // return;
        postHttp($http, 'installtionCase/commitOcrDetail', postData, function (res) {
            if (res.code === 0 && res.msg == "success") {
                $scope.removeStorge();
                $scope.$applyAsync();
                $scope.one();
            } else {
                // alert(res.msg);
                $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
            }
        });

    };

    //清除storage数据
    $scope.removeStorge = function () {
        var token = getToken();
        localStorage.removeItem("sdm-feSignUrl");
        localStorage.removeItem("sdm-customerSignUrl");
        localStorage.removeItem("sdm-ocrUsageInfoPage");
        localStorage.removeItem("sdm-ocrSupplyStatusPage");
        localStorage.removeItem("sdm-ocrConfigPage");
        localStorage.removeItem("sdm-caseNum");
        sessionStorage.setItem("saveCheck", token);
    }

    //拍照
    $scope.takePhoto = function () {
        if($scope.imageUrls.length >= 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传9张图片"));
            return;
        }
        /*$scope.loadindex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/
        var scanType = "sdm2UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
        	//context.uploadImage(getToken(),scanType);
			window.webkit.messageHandlers.uploadImage.postMessage({"scanType":scanType});
        }else{
			hm.uploadImage(getToken(),scanType);
		}
    }

    //从相册选择图片
    $scope.takePhotoFromSystem = function () {
        if($scope.imageUrls.length >= 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传9张图片"));
            return;
        }
        var scanType = "sdm2UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImageFromPhoto(getToken(),scanType);
        }else if(checkPhone("iphone")) {
            $rootScope.tipPopups.show(($rootScope.tipMessage = "暂不支持"));
            return;
           // context.uploadImage(getToken(), scanType);
        }else {
			hm.uploadImageFromPhoto(getToken(),scanType,$scope.imageUrls.length);
        }
        /*$scope.loadindex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/
    }


    //存入数组图片路径
    $scope.photoResult = function (url) {
        //关闭加载
        // layer.close($scope.loadindex);
        //if (navigator.onLine){ //正常工作
            alert(JSON.stringify(url));
            if(typeof(url)=='string'){
               var jsonData = JSON.parse(url).imageurl;
               for (var i = 0; i < jsonData.length; i++) {
                   var counter = jsonData[i].url;
               	 $scope.imageUrls.push(counter);
               }
            }else{
                $scope.imageUrls.push(url.imageurl);
            }
            $scope.$applyAsync();
        /*}else { //执行离线状态时的任务
            $rootScope.tipPopups.show(($rootScope.tipMessage = "当前网络不佳，稍后再试"));
            return;
        }*/
    }
    
    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }

    //初始化
    function init() {
        //案例id
        $scope.caseId = five.params.caseId;
        if (!five.params.caseId) {
            $scope.caseId = localStorage.getItem("sdm-caseId");
        }
        $scope.blhLong = five.params.customeBlhLong;
        $scope.blhLat = five.params.customeBlhLat;
        $scope.ocrStatusReportPage = {};//JSON.parse(localStorage.getItem("sdm-ocrStatusReportPage"));
        $scope.ocrConfigPage = {};
        $scope.ocrUsageInfoPage = [];
        $scope.ocrSupplyStatusPage = {};

        if(localStorage.getItem("sdm-ocrUsageInfoPage")){
            $scope.ocrUsageInfoPage = JSON.parse(localStorage.getItem("sdm-ocrUsageInfoPage"));
        }
        if(localStorage.getItem("sdm-ocrConfigPage")){
            $scope.ocrConfigPage = JSON.parse(localStorage.getItem("sdm-ocrConfigPage"));
        }
        if(localStorage.getItem("sdm-ocrSupplyStatusPage")){
            $scope.ocrSupplyStatusPage = JSON.parse(localStorage.getItem("sdm-ocrSupplyStatusPage"));
        }

        $scope.imageUrls = [];

        $scope.feSignUrl = localStorage.getItem("sdm-feSignUrl");
        $scope.customerSignUrl = localStorage.getItem("sdm-customerSignUrl");
    }
    init();

    reloadJs();

});
//返回扫描结果
function sdm2UploadImageResult(msg) {
    var $scope = getScope("five");
    $scope.photoResult(msg);
}